package com.wuhan.template.module.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseFragment;
import com.wuhan.template.constant.Urls;
import com.wuhan.template.module.home.adapter.JiemuDanAdapter;
import com.wuhan.template.module.home.entity.JieMuListViewEntity;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/12 0012.
 */
public class JieMuDanDetailFragment extends BaseFragment {

    @BindView(R.id.jiemudan_pullListView)
    protected PullToRefreshListView listView;

    private JiemuDanAdapter adapter;

    private List<JieMuListViewEntity> listViewEntities;
    private String type;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_jiemudan_detail;
    }

    public static JieMuDanDetailFragment newInstance(String type, String id) {

        Bundle args = new Bundle();
        JieMuDanDetailFragment fragment = new JieMuDanDetailFragment();
        args.putString("type", type);
        args.putString("dianshiId", id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getReqParams(Bundle bundle) {
        type = bundle.getString("type");
        dianshiId = bundle.getString("dianshiId");
    }

    @Override
    protected void initViews() {
        listViewEntities = new ArrayList<>();
        adapter = new JiemuDanAdapter(getContext(), listViewEntities);
        listView.setAdapter(adapter);
        adapter.setOnItemClickListener(new JiemuDanAdapter.OnVideoClickListener() {
            @Override
            public void onVideoItemClick(View view, String m3u8, String dianshiId) {
                Intent intent = new Intent();
                intent.putExtra("m3u8", m3u8);
                intent.putExtra("dianshiId", dianshiId);
                getContext().sendBroadcast(intent);
                if(listener!=null){
                listener.onArticleSelected( m3u8, dianshiId);
            }}
        });
    }

    private int channel_id = 0;
    private String dianshiId;

    @Override
    protected void loadData() {
        switch (type) {
            case 0 + "":
                OkHttpUtils.get(Urls.JIEMUDAN + "&channel_id=" + dianshiId + "&zone=" + -5, null);
                break;
            case 1 + "":
                OkHttpUtils.get(Urls.JIEMUDAN + "&channel_id=" + dianshiId + "&zone=" + -4, null);
                break;
            case 2 + "":
                OkHttpUtils.get(Urls.JIEMUDAN + "&channel_id=" + dianshiId + "&zone=" + -3, null);
                break;
            case 3 + "":
                OkHttpUtils.get(Urls.JIEMUDAN + "&channel_id=" + dianshiId + "&zone=" + -2, null);
                break;
            case 4 + "":
                OkHttpUtils.get(Urls.JIEMUDAN + "&channel_id=" + dianshiId + "&zone=" + -1, null);
                break;
            case 5 + "":
                OkHttpUtils.get(Urls.JIEMUDAN + "&channel_id=" + dianshiId + "&zone=" + channel_id, null);
                break;
            case 6 + "":
                OkHttpUtils.get(Urls.JIEMUDAN + "&channel_id=" + dianshiId + "&zone=" + 1, null);
                break;
        }

    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {

        Gson gson = new Gson();
        int zone = 0;
        switch (type) {
            case 0 + "":
                zone = -5;
                break;
            case 1 + "":
                zone = -4;
                break;
            case 2 + "":
                zone = -3;
                break;
            case 3 + "":
                zone = -2;
                break;
            case 4 + "":
                zone = -1;
                break;
            case 5 + "":
                zone = 0;
                break;
            case 6 + "":
                zone = 1;
                break;
        }
        if (message.type.contains(
                Urls.JIEMUDAN + "&channel_id=" + dianshiId + "&zone=" + zone)) {
            JieMuListViewEntity[] jieMuListViewEntities =
                    gson.fromJson(message.result, JieMuListViewEntity[].class);

            //JieMuListViewEntity jieMuListViewEntity = gson.fromJson(message.result, JieMuListViewEntity.class);

            //listViewEntities.clear();
            listViewEntities.addAll(Arrays.asList(jieMuListViewEntities));
            adapter.notifyDataSetChanged();
        }

    }

    private OnArticleSelectedListener listener = null;
    public interface OnArticleSelectedListener{
         void onArticleSelected(String m3u8,String dianshiId);
    }

    public void setOnArticleSelectedListener(OnArticleSelectedListener listener){
        this.listener = listener;
    }
}
